package com.mdlib.helper.module.send.profile.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.core.util.LogUtil;
import com.ljlib.core.util.common.RegularUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.AddressEntity;
import com.mdlib.helper.ui.widget.togglebutton.ToggleButton;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

import static com.mdlib.helper.ui.UIHelper.ADDRESS_RELATE;

/**
 * 作者：HQY on 16/11/16 13:16
 * 邮箱：hqy_xz@126.com
 */

public class AddressRelateFragment extends BaseTitleFragment {


    @Bind(R.id.et_address_name)
    EditText mEtAddressName;
    @Bind(R.id.et_address)
    EditText mEtAddress;
    @Bind(R.id.et_address_phone)
    EditText mEtAddressPhone;
    @Bind(R.id.tb_set_address)
    ToggleButton mTbSetAddress;
    @Bind(R.id.tv_new_add_ok)
    TextView mTvNewAddOk;
    private AddressEntity address;
    private String type = "1";

    public static AddressRelateFragment newInstance() {
        return new AddressRelateFragment();

    }

    public static AddressRelateFragment newInstance(AddressEntity addressEntity) {
        AddressRelateFragment fragment = new AddressRelateFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ADDRESS_RELATE, addressEntity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            address = (AddressEntity) bundle.getSerializable(ADDRESS_RELATE);
        }
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_address_relate;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        if (EmptyUtils.isEmpty(address)) {
            setTitle(R.string.address_tv_new_add_address, R.color.white)
                    .setTitleBgColor(R.color.generic_view_bg)
                    .setImgLeftListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            removeFragment();
                        }
                    });
        } else {
            setTitle(R.string.address_right_tv_edit_title, R.color.white)
                    .setTitleBgColor(R.color.generic_view_bg)
                    .setImgLeftListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            removeFragment();
                        }
                    });
            updateAddress(address);
        }
        init();
    }


    public void updateAddress(AddressEntity addressInfo) {
        LogUtil.e(addressInfo.toString());
        mEtAddressName.setText(addressInfo.getName());
        mEtAddress.setText(addressInfo.getAddress());
        mEtAddressPhone.setText(addressInfo.getPhoneNumber());
        if (addressInfo.getType() == 1) {
            mTbSetAddress.setToggleOn();
        } else {
            mTbSetAddress.setToggleOff();
        }
    }

    public void init() {
        mTbSetAddress.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on) {
                    type = "1";
                } else {
                    type = "0";
                }
            }
        });
    }

    @OnClick(R.id.tv_new_add_ok)
    public void onClick() {
        String name = mEtAddressName.getText().toString();
        String addresStr = mEtAddress.getText().toString();
        String phoneNumber = mEtAddressPhone.getText().toString();
        if (EmptyUtils.isNotEmpty(name)) {
            if (EmptyUtils.isNotEmpty(addresStr)) {
                if (EmptyUtils.isNotEmpty(phoneNumber)) {
                    if (RegularUtils.isMobileExact(phoneNumber)) {
                        String id = "";
                        if (EmptyUtils.isNotEmpty(address)) {
                            id = String.valueOf(address.getAddressId());
                        }
                        startLoadingDialog(true);
                        addSubscribe(DataManager.getInstance().getUserApi()
                                .updateAddress(id, name, addresStr, phoneNumber, type)
                                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                                .subscribe(new RxSubscribe<BaseResult>() {
                                    @Override
                                    protected void onSucc(BaseResult baseResult) {
                                        stopLoadingDialog();
                                        ToastUtil.showToast(baseResult.msg);
                                        removeFragment();
                                    }

                                    @Override
                                    protected void onFail(String msg) {
                                        stopLoadingDialog();
                                        ToastUtil.showToast(msg);
                                    }
                                }));
                    } else {
                        ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_phone_form));
                    }

                } else {
                    ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_phone_not_empty));
                }
            } else {
                ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_address_not_empty));
            }
        } else {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_nickname_not_empty));
        }

    }
}
